import Util from '../..//utils/util.js';

let app = getApp();
Component({
  properties: {
    swiperType: {
      type: String,
      value: ""
    },
    imgUrls: {
      type: Object,
      value: [],
      observer: function (newVal, oldVal) {
        this.setData({
          currentSwiper: 0,
        })
        if (newVal) {
          this.setData({
            showDot: (newVal[0] && newVal[0].is_qrcode == 1) ? false : true
          })
          this.triggerEvent("callSwiper", newVal[0]);
        }
      }
    },
    swiperClass: {
      type: String,
      value: ''
    },
    interval: {
      type: Number,
      value: 3000
    },
    showEdit: {
      type: Boolean,
      value: false
    },
    location: {
      type: String,
      value: ''
    },
    is_dot_num: {
      type: Number,
      value: 0
    }
  },
  data: {
    circular: true,
    autoplay: false,
    interval: 3000,
    duration: 500,
    currentSwiper: 0,
    showDot: true
  },

  attached: function () {

  },

  methods: {

    init() {
      this.setData({
        currentSwiper: 0,
        imgUrls: []
      })
    },

    swiperChange: function (e) {

      this.setData({
        currentSwiper: e.detail.current,
        showDot: true
      });

      if (this.data.imgUrls[this.data.currentSwiper] && this.data.imgUrls[this.data.currentSwiper].is_qrcode == 1) {
        this.setData({
          showDot: false
        })
        this.triggerEvent("callSwiper", this.data.imgUrls[this.data.currentSwiper]);
      } else {
        this.triggerEvent("callSwiper", this.data.imgUrls[this.data.currentSwiper]);
      }

    },

    imgYu: function (event) {
      let imgList = this.data.imgUrls;
      const key = event.currentTarget.dataset.key
      let src = "";
      if (!Util.isEmpty(this.data.imgUrls[key]))
        src = Util.previewCdnImage(this.data.imgUrls[key].path, 750, 0, 1)

      const urls = [];
      for (let i = 0; i < imgList.length; i++) {
        urls.push(Util.previewCdnImage(imgList[i]['path'], 750, 0, 1));
      }

      if (urls) {
        wx.previewImage({
          current: src,
          urls: urls,
          showmenu: false
        });
      }

    }
  }
});
